//
//  YDASearchHistoryCell.m
//  siyanone
//
//  Created by LK-IOS on 2018/3/27.
//  Copyright © 2018年 LK-News. All rights reserved.
//

#import "YDASearchHistoryCell.h"

@interface YDASearchHistoryCell()

@end

@implementation YDASearchHistoryCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *aSearchHistoryCell = @"YDASearchHistoryCell";
    YDASearchHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:aSearchHistoryCell];
    if (cell == nil) {
        cell = [[YDASearchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aSearchHistoryCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.backgroundColor = [UIColor whiteColor];
        
        
    }
    return self;
}
-(void)setArray:(NSArray *)array
{
    _array = array;
    
    if (array.count == 0) {
        return;
    }
    
    
    
    //每行最多显示个数
    NSInteger colNumber = 4;
    //按钮上下间距
    CGFloat buttonUDMargin;
    //按钮宽度
    CGFloat buttonW;
    //按钮左右间距
    CGFloat buttonLRMargin = rate375(20);
    buttonW = (LKScreenW - (colNumber + 1) * buttonLRMargin) / colNumber;
    
    //按钮的高度
    CGFloat buttonH;
    
    if (isPhoneX) {
        buttonUDMargin = rateX812(10);
        buttonH = rateX812(22);
    }else{
        buttonUDMargin = rate750(10);
        buttonH = rate750(22);
    }
    
    for (int i = 0; i < array.count; i++) {
        //确定第几列第几行
        NSInteger whichCow = i % colNumber;
        NSInteger whichCol = i / colNumber;
        //确定每个空间的X
        CGFloat x = (whichCow + 1) * buttonLRMargin + whichCow * buttonW;
        
        //确定每个空间的Y
        CGFloat y = (whichCol + 1) * buttonUDMargin + whichCol * buttonH;
        SYNoHeightedButton *button = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:button];
        button.frame = CGRectMake(x, y, buttonW, buttonH);
        button.backgroundColor = [UIColor clearColor];
        button.tag = i;
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:NumberColor(104, 114, 132) forState:UIControlStateNormal];
        button.titleLabel.textAlignment = NSTextAlignmentLeft;
        button.titleLabel.font = [UIFont systemFontOfSize:font16ScreenWRate * LKScreenW];
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    }
}

-(void)buttonClick:(SYNoHeightedButton *)button
{
    if ([self.delegate respondsToSelector:@selector(sSearchHistoryCell:didClickButton:)]) {
        [self.delegate sSearchHistoryCell:self didClickButton:button.tag];
    }
}



@end
